﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;

namespace Ict.DataAccess.Linq
{
    public class ModifiableProvider: ExecutableProvider, IQueryProvider
    {
        #region IQueryProvider 成员

        public virtual IQueryable<TElement> CreateQuery<TElement>(System.Linq.Expressions.Expression expression)
        {
            var exp = expression as MethodCallExpression;
            var modifier = (exp.Arguments[0] as ConstantExpression).Value as Modifiable;

            modifier.AddExpression(exp.Method.Name, exp.Arguments[1]);

            if (modifier.ElementType == typeof(TElement))
                return (IQueryable<TElement>)modifier;
            else
                return (IQueryable<TElement>)modifier.Clone<TElement>();
        }

        public virtual IQueryable CreateQuery(System.Linq.Expressions.Expression expression)
        {
            var exp = expression as MethodCallExpression;
            var modifier = (exp.Arguments[0] as ConstantExpression).Value as Modifiable;

            modifier.AddExpression(exp.Method.Name, expression);

            return modifier;
        }

        public TResult Execute<TResult>(System.Linq.Expressions.Expression expression)
        {
            throw new NotSupportedException("不执行查询操作。");
        }

        public object Execute(System.Linq.Expressions.Expression expression)
        {
            throw new NotSupportedException("不执行查询操作。");
        }

        #endregion
    }
}
